import { Context } from "koa";
import { IdDTO } from "../validators/request.validator";
import { DishesIdListRequestDTO } from "../validators/dishes.validator";
import { WishService } from "../services/wish.service";
import {
  CreateWishSchema,
  DeleteWishSchema,
} from "../validators/wish.validator";
import { AuthUtil, TokenPayload } from "../utils/auth.util";

export class WishController {
  // 查询所有用户下周想吃的菜品列表
  public static async listAllWishes(ctx: Context) {
    try {
      let wishList;
      // 如果有token则菜品中增加isWished字段
      const tokenPayload = AuthUtil.getTokenPayLoad(ctx);
      if (tokenPayload) {
        const { userId } = tokenPayload as TokenPayload;
        wishList = await WishService.listAllWithIsWished(userId);
      } else {
        wishList = await WishService.listAll();
      }

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: "查询成功",
        data: wishList,
      };
    } catch (error) {
      console.log("查询想吃的榜失败", error);

      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : "查询想吃的榜失败",
        data: null,
      };
    }
  }

  /**
   * 查询用户下周想吃的菜品列表
   */
  public static async listWishes(ctx: Context) {
    const { id: userId } = ctx.state.validatedParams as IdDTO;

    try {
      const wishes = await WishService.list(userId);

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: "查询成功",
        data: wishes,
      };
    } catch (error) {
      console.log("查询我想吃失败", error);

      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : "查询我想吃失败",
        data: null,
      };
    }
  }

  /**
   * 创建/修改用户（下周）想吃的菜品
   */
  public static async upsertWishes(ctx: Context) {
    const { id: userId } = ctx.state.validatedParams as IdDTO;
    const { dishesIdList } = ctx.state.validatedBody as DishesIdListRequestDTO;

    try {
      const wishes = await WishService.upsert(userId, dishesIdList);

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: "创建/修改成功",
        data: wishes,
      };
    } catch (error) {
      console.log("更新我想吃失败", error);

      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : "更新我想吃失败",
        data: null,
      };
    }
  }

  // 添加菜品到下周想吃
  public static async add(ctx: Context) {
    const { userId } = ctx.state.authorization as TokenPayload;
    const { dishesId } = ctx.state.validatedBody as CreateWishSchema;

    try {
      const wish = await WishService.add(dishesId, userId);

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: "添加成功",
        data: wish,
      };
    } catch (error) {
      console.log("添加我想吃失败", error);

      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : "添加我想吃失败",
        data: null,
      };
    }
  }

  // 删除用户下周想吃的菜品
  public static async remove(ctx: Context) {
    try {
      const { userId } = ctx.state.authorization as TokenPayload;
      const { dishesId } = ctx.state.validatedParams as DeleteWishSchema;
      await WishService.remove(dishesId, userId);

      ctx.status = 204;
      ctx.body = {
        code: 204,
        message: "删除成功",
        data: null,
      };
    } catch (error) {
      console.log("删除我想吃失败", error);

      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : "删除我想吃失败",
        data: null,
      };
    }
  }
}
